package androidx.test.espresso;

import android.content.Context;
import android.os.Build;
import android.os.Looper;
import android.view.View;
import android.view.ViewConfiguration;
import androidx.test.espresso.action.ViewActions;
import androidx.test.espresso.base.IdlingResourceRegistry;
import androidx.test.espresso.core.internal.deps.guava.base.Preconditions;
import androidx.test.espresso.core.internal.deps.guava.util.concurrent.ListenableFutureTask;
import androidx.test.espresso.matcher.ViewMatchers;
import androidx.test.espresso.util.TreeIterables;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.FutureTask;
import org.hamcrest.Matcher;
import org.hamcrest.Matchers;

/* loaded from: classes.dex */
public final class Espresso {
    private static final BaseLayerComponent BASE;
    private static final Matcher<View> OVERFLOW_BUTTON_MATCHER;
    private static final IdlingResourceRegistry baseRegistry;

    static {
        BaseLayerComponent baseLayer = GraphHolder.baseLayer();
        BASE = baseLayer;
        baseRegistry = baseLayer.idlingResourceRegistry();
        OVERFLOW_BUTTON_MATCHER = Matchers.anyOf(Matchers.allOf(ViewMatchers.isDisplayed(), ViewMatchers.withContentDescription("More options")), Matchers.allOf(ViewMatchers.isDisplayed(), ViewMatchers.withClassName(Matchers.endsWith("OverflowMenuButton"))));
    }

    private Espresso() {
    }

    public static ViewInteraction onView(final Matcher<View> viewMatcher) {
        return BASE.plus(new ViewInteractionModule(viewMatcher)).viewInteraction();
    }

    public static DataInteraction onData(Matcher<? extends Object> dataMatcher) {
        return new DataInteraction(dataMatcher);
    }

    @Deprecated
    public static void registerLooperAsIdlingResource(Looper looper) {
        registerLooperAsIdlingResource(looper, false);
    }

    @Deprecated
    public static void registerLooperAsIdlingResource(Looper looper, boolean considerWaitIdle) {
        IdlingRegistry.getInstance().registerLooperAsIdlingResource(looper);
        baseRegistry.sync(IdlingRegistry.getInstance().getResources(), IdlingRegistry.getInstance().getLoopers());
    }

    @Deprecated
    public static boolean registerIdlingResources(IdlingResource... resources) {
        if (!IdlingRegistry.getInstance().register(resources)) {
            return resources.length == 0;
        }
        baseRegistry.sync(IdlingRegistry.getInstance().getResources(), IdlingRegistry.getInstance().getLoopers());
        return true;
    }

    @Deprecated
    public static boolean unregisterIdlingResources(IdlingResource... resources) {
        if (!IdlingRegistry.getInstance().unregister(resources)) {
            return resources.length == 0;
        }
        baseRegistry.sync(IdlingRegistry.getInstance().getResources(), IdlingRegistry.getInstance().getLoopers());
        return true;
    }

    @Deprecated
    public static List<IdlingResource> getIdlingResources() {
        return baseRegistry.getResources();
    }

    public static void setFailureHandler(FailureHandler failureHandler) {
        BASE.failureHolder().update((FailureHandler) Preconditions.checkNotNull(failureHandler));
    }

    public static void closeSoftKeyboard() {
        onView(ViewMatchers.isRoot()).perform(ViewActions.closeSoftKeyboard());
    }

    public static void openContextualActionModeOverflowMenu() {
        onView(ViewMatchers.isRoot()).perform(new TransitionBridgingViewAction());
        onView(OVERFLOW_BUTTON_MATCHER).perform(ViewActions.click(ViewActions.pressBack()));
    }

    public static void pressBack() {
        onView(ViewMatchers.isRoot()).perform(ViewActions.pressBack());
    }

    public static void pressBackUnconditionally() {
        onView(ViewMatchers.isRoot()).perform(ViewActions.pressBackUnconditionally());
    }

    public static void openActionBarOverflowOrOptionsMenu(Context context) {
        if (context.getApplicationInfo().targetSdkVersion < 11) {
            onView(ViewMatchers.isRoot()).perform(ViewActions.pressMenuKey());
        } else if (hasVirtualOverflowButton(context)) {
            onView(ViewMatchers.isRoot()).perform(new TransitionBridgingViewAction());
            onView(OVERFLOW_BUTTON_MATCHER).perform(ViewActions.click());
        } else {
            onView(ViewMatchers.isRoot()).perform(ViewActions.pressMenuKey());
        }
    }

    public static <T> T onIdle(Callable<T> action) {
        Executor mainThreadExecutor = BASE.mainThreadExecutor();
        ListenableFutureTask create = ListenableFutureTask.create(new Runnable() { // from class: androidx.test.espresso.Espresso.1
            @Override // java.lang.Runnable
            public void run() {
                Espresso.BASE.uiController().loopMainThreadUntilIdle();
            }
        }, null);
        FutureTask futureTask = new FutureTask(action);
        create.addListener(futureTask, mainThreadExecutor);
        mainThreadExecutor.execute(create);
        try {
            create.get();
            return (T) futureTask.get();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e2) {
            if (e2.getCause() instanceof AppNotIdleException) {
                throw ((AppNotIdleException) e2.getCause());
            }
            throw new RuntimeException(e2);
        }
    }

    public static void onIdle() {
        onIdle(new Callable<Void>() { // from class: androidx.test.espresso.Espresso.2
            @Override // java.util.concurrent.Callable
            public Void call() throws Exception {
                return null;
            }
        });
    }

    private static boolean hasVirtualOverflowButton(Context context) {
        if (Build.VERSION.SDK_INT < 14) {
            return Build.VERSION.SDK_INT >= 11;
        }
        return !ViewConfiguration.get(context).hasPermanentMenuKey();
    }

    /* loaded from: classes.dex */
    private static class TransitionBridgingViewAction implements ViewAction {
        @Override // androidx.test.espresso.ViewAction
        public String getDescription() {
            return "Handle transition between action bar and action bar context.";
        }

        private TransitionBridgingViewAction() {
        }

        @Override // androidx.test.espresso.ViewAction
        public void perform(UiController controller, View view) {
            int i = 0;
            while (isTransitioningBetweenActionBars(view) && i < 100) {
                i++;
                controller.loopMainThreadForAtLeast(50L);
            }
        }

        @Override // androidx.test.espresso.ViewAction
        public Matcher<View> getConstraints() {
            return ViewMatchers.isRoot();
        }

        private boolean isTransitioningBetweenActionBars(View view) {
            int i = 0;
            for (View view2 : TreeIterables.breadthFirstViewTraversal(view)) {
                if (Espresso.OVERFLOW_BUTTON_MATCHER.matches(view2)) {
                    i++;
                }
            }
            return i > 1;
        }
    }
}
